from selenium.webdriver.common.by import By
from common.Utils import BlogDriver
from tests.BlogLogin import BlogLoginDriver
from time import sleep

# 测试简墨首页
class BlogList:
    url = ""
    driver = ""
    def __init__(self):
        self.url = "http://8.137.19.140:9090/blog_list.html"
        self.driver = BlogDriver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()
    # 登录状态下的简墨列表的测试用例
    def ListByLogin(self):
        # 查找简墨博客列表元素：标题、发布时间、内容、"查看详情"按钮
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.title")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.date")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > div.desc")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(1) > a")
        # 查找简墨博客个人信息：头像、昵称、文章、文章数量、分类、分类数量
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > img")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(5) > span:nth-child(1)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(4) > span:nth-child(2)")
        self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > div:nth-child(5) > span:nth-child(2)")
        # 屏幕截图记录
        BlogDriver.getScreeShot()
        # 打印成功相关信息
        print("ListByLogin:>"+self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.left > div > h3").text)
        print("ListByLogin:>"+self.driver.find_element(By.CSS_SELECTOR, "body > div.container > div.right > div:nth-child(2) > div.title").text)
    # 未登录状态下的简墨列表的测试用例
    def ListByNotLogin(self):
        # 会自动跳转回简墨登录页
        sleep(0.5)
        current_url = self.driver.current_url
        assert current_url != self.url
        print("ListByNotLogin-before_url:>"+self.url)
        print("ListByNotLogin-after_url :>"+current_url)
        # 屏幕截图记录
        BlogDriver.getScreeShot()

BlogListDriver = BlogList()
# BlogListDriver.ListByNotLogin()
# BlogLoginDriver.LoginSucTest()
# BlogListDriver.ListByLogin()
# BlogDriver.driver.quit()